/*
 * yarf.h
 *
 * Copyright 2011 Pieter Agten
 *
 * This file is part of Yarf.
 *
 * Yarf is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Yarf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Yarf.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   yarf.h
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   3 sep 2011
 * @brief  Defines a number of macros and types that will be used in several
 *         other components.
 */

#ifndef YARF_H
#define YARF_H

#include "config.h"
#include "boards.h"

/**
 * The number of axes of the machine.
 */
#define NUM_AXES  4

/**
 * The number of positional axes of the machine.
 */
#define NUM_POS_AXES  3

/**
 * Index number of the X axis.
 */
#define X_AXIS     0

/**
 * Index number of the Y axis.
 */
#define Y_AXIS     1

/**
 * Index number of the Z axis.
 */
#define Z_AXIS     2

/**
 * Index number of the E axis.
 */
#define E_AXIS     3


/**
 * Index number of the positive X axis direction.
 */
#define X_AXIS_POS X_AXIS

/**
 * Index number of the positive Y axis direction.
 */
#define Y_AXIS_POS Y_AXIS

/**
 * Index number of the positive Z axis direction.
 */
#define Z_AXIS_POS Z_AXIS

/**
 * Index number of the positive E axis direction.
 */
#define E_AXIS_POS E_AXIS

/**
 * Index number of the negative X axis direction.
 */
#define X_AXIS_NEG 4

/**
 * Index number of the negative Y axis direction.
 */
#define Y_AXIS_NEG 5

/**
 * Index number of the negative Z axis direction.
 */
#define Z_AXIS_NEG 6

/**
 * Index number of the negative E axis direction.
 */
#define E_AXIS_NEG 7

/**
 * The number of milimeters moved when taking a step on the X axis.
 */
#define X_MM_PER_STEP      (1.0/X_STEPS_PER_MM)

/**
 * The number of milimeters moved when taking a step on the Y axis.
 */
#define Y_MM_PER_STEP      (1.0/Y_STEPS_PER_MM)

/**
 * The number of milimeters moved when taking a step on the Z axis.
 */
#define Z_MM_PER_STEP      (1.0/Z_STEPS_PER_MM)

/**
 * The number of milimeters moved when taking a step on the E axis.
 */
#define E_MM_PER_STEP      (1.0/E_STEPS_PER_MM)


/* Extruder feed rate calculations */

/**
 * The radius (in mm) of the filament going into the extruder.
 */
#define R_IN  (FD/2)

/**
 * The radius (in mm) of the filament coming out of the extruder's nozzle.
 */
#define R_OUT (ED/2)

/**
 * The area (in mm²) of the filament going into the extruder.
 */
#define FILAMENT_INPUT_AREA (M_PI * R_IN * R_IN)

/**
 * The area (in mm²) of the filament coming out of the extruder.
 */
#define FILAMENT_OUTPUT_AREA (M_PI * R_OUT * R_OUT)

/**
 * The length of filament (in mm) pushed in by one rotation of the extruder's
 * pinch wheel.
 */
#define FL (M_PI * PWD)

/**
 * The length of filament (in mm) pushed out of the extruder's nozzle by one
 * rotation of its pinch wheel.
 */
#define EL ( (FL * R_IN * R_IN)/(R_OUT * R_OUT) )

/**
 * The number of steps it takes the extruder's stepper motor to push in 1 mm of
 * filament.
 */
#define E_STEPS_PER_MM_IN  (E_STEPS_PER_REV * GEAR_RATIO / FL)

/**
 * The number of steps it takes the extruder's stepper motor to push out 1 mm
 * of filament.
 */
#define E_STEPS_PER_MM_OUT (E_STEPS_PER_MM_IN * FL / EL)

/**
 * The maximum continuous speed (in mm/min) of filament coming out of the
 * extruder. This rate can be exceeded when the extruder performs advance steps
 * due to the the advance pressure management system (if enabled).
 */
#define E_MAX_SPEED_OUT    (E_MAX_SPEED_IN * FL / EL)

/**
 * The absolute maximum speed (in mm/min) of filament coming out of the
 * extruder. This rate will never be exceeded, even when the extruder is
 * performing advance steps due to the advance pressure management system.
 */
#define E_MAX_ADVANCE_OUT  (E_MAX_ADVANCE_SPEED_IN * FL / EL)


#if E_COORDINATES_ARE_INPUT
#define E_STEPS_PER_MM           E_STEPS_PER_MM_IN
#define E_MAX_SPEED              E_MAX_SPEED_IN
#define E_MAX_ADVANCE_SPEED      E_MAX_ADVANCE_SPEED_IN
#else
#define E_STEPS_PER_MM           E_STEPS_PER_MM_OUT
#define E_MAX_SPEED              E_MAX_SPEED_OUT
#define E_MAX_ADVANCE_SPEED      E_MAX_ADVANCE_SPEED_OUT
#endif

/**
 * Name of the stream to use for reading data from and writing data to the host
 * computer.
 */
#define HOST_USART usart0_stream

/**
 * Type definition for a number of steps.
 */
typedef long steps_t;

/**
 * Type definition for a speed (feed rate).
 */
typedef unsigned int speed_t;

#endif // YARF_H
